/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aksw.treelearning.tree;

import org.aksw.treelearning.data.Cache;
import org.aksw.treelearning.data.Mapping;
import org.aksw.treelearning.data.SetOperations;
import org.aksw.treelearning.utils.Utils;

/**
 *
 * @author ngonga
 */
public class Node {

    public Node yesChild;
    public Node noChild;
    public Mapping mapping;
    private boolean negation;
    private boolean leafNode;
    public Condition condition;

    public Condition getCondition() {
        return condition;
    }

    public void setCondition(Condition condition) {
        this.condition = condition;
    }

    public boolean isLeafNode() {
        return leafNode;
    }

    public void setLeafNode(boolean leafNode) {
        this.leafNode = leafNode;
    }

    public boolean isNegation() {
        return negation;
    }

    public void setNegation(boolean negation) {
        this.negation = negation;
    }

    public Node() {
        yesChild = null;
        noChild = null;
        mapping = null;
    }

    public Mapping getMapping() {
        return mapping;
    }

    public void setMapping(Mapping mapping) {
        this.mapping = mapping;
    }

    public boolean mappingNegation() {
        return negation;
    }

    public Node getNoChild() {
        return noChild;
    }

    public void setNoChild(Node noChild) {
        this.noChild = noChild;
        if (noChild != null || yesChild != null) {
            leafNode = false;
        } else {
            leafNode = true;
        }
    }

    public Node getYesChild() {
        return yesChild;
    }

    public void setYesChild(Node yesChild) {
        this.yesChild = yesChild;
        if (noChild != null || yesChild != null) {
            leafNode = false;
        } else {
            leafNode = true;
        }
    }

    public void propagateMapping(Cache source, Cache target) {
        if (!leafNode) {
            //1. Compute mapping
            Mapping m = Utils.computeMapping(source, target, condition);
            //2. yes child mapping = own mapping intersected with novel mapping
            yesChild.mapping = SetOperations.intersection(m, mapping);
            noChild.mapping = SetOperations.difference(mapping, m);
            yesChild.propagateMapping(source, target);
            noChild.propagateMapping(source, target);
        }
    }
}
